package com.lishuang.pet.service.impl;

import com.lishuang.basic.exception.BusinessException;
import com.lishuang.basic.service.IBaiduAiAuditService;
import com.lishuang.basic.service.impl.BaseServiceImpl;
import com.lishuang.basic.util.LoginContext;
import com.lishuang.org.domain.Employee;
import com.lishuang.org.mapper.EmployeeMapper;
import com.lishuang.pet.domain.Pet;
import com.lishuang.pet.domain.PetDetail;
import com.lishuang.pet.domain.PetOnlineAuditLog;
import com.lishuang.pet.mapper.PetDetailMapper;
import com.lishuang.pet.mapper.PetMapper;
import com.lishuang.pet.mapper.PetOnlineAuditLogMapper;
import com.lishuang.pet.service.IPetService;
import com.lishuang.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class PetServiceImpl extends BaseServiceImpl<Pet> implements IPetService {

    @Autowired
    private PetDetailMapper petDetailMapper;

    @Autowired
    private PetMapper petMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private IBaiduAiAuditService baiduAiAuditService;

    @Autowired
    private PetOnlineAuditLogMapper petOnlineAuditLogMapper;

    /**
     * 宠物管理合并宠物详情保存
     */
    @Override
    public void save(Pet pet) {
        super.save(pet);
        PetDetail petDetail = pet.getPetDetail();
        if (petDetail != null) {
            petDetail.setPetId(pet.getId());
            petDetailMapper.save(petDetail);
        }
    }

    /**
     * 宠物管理合并宠物详情删除
     */
    @Override
    public void remove(Long id) {
        super.remove(id);
        petDetailMapper.removeByPetId(id);
    }

    /**
     * 宠物管理合并宠物详情pi批量删除
     */
    @Override
    public void batchDelete(List<Long> ids) {
        for (Long id : ids) {
            remove(id);
        }
    }

    /**
     * 宠物管理合并宠物详情修改
     */
    @Override
    public void update(Pet pet) {
        super.update(pet);
        PetDetail petDetail = pet.getPetDetail();
        if (petDetail != null) {
            petDetail.setPetId(pet.getId());
            petDetailMapper.updateByPetId(petDetail);
        }
    }

    /**
     * 宠物管理上架
     */
    @Override
    public void onsale(List<Long> ids, HttpServletRequest request) {
        //获取登录信息
        Logininfo logininfo = LoginContext.getLoginUser(request);
        //获取审核人
        Employee employee = employeeMapper.loadByLogininfoId(logininfo.getId());
        for (Long id : ids) {
            Pet pet = petMapper.loadById(id);
            //1.上架之前对文本和图片做审核
            Boolean textBoo = baiduAiAuditService.textAudit(pet.getName());
            Boolean imageBoo = baiduAiAuditService.imageAudit(pet.getResources());
            if (textBoo && imageBoo) {
                pet.setOnsaletime(new Date());
                pet.setState(1);
                //上架成功，修改上架时间和状态
                petMapper.update(pet);

                //添加审核信息表
                PetOnlineAuditLog petOnlineAuditLog = new PetOnlineAuditLog();
                petOnlineAuditLog.setPetId(pet.getId());
                petOnlineAuditLog.setState(1);
                petOnlineAuditLog.setAuditId(employee.getId());
                petOnlineAuditLog.setAuditTime(new Date());
                petOnlineAuditLog.setNote("审核通过");
                petOnlineAuditLogMapper.save(petOnlineAuditLog);
            } else {

                //添加审核信息表
                PetOnlineAuditLog petOnlineAuditLog = new PetOnlineAuditLog();
                petOnlineAuditLog.setPetId(pet.getId());
                petOnlineAuditLog.setState(-1);
                petOnlineAuditLog.setAuditId(employee.getId());
                petOnlineAuditLog.setAuditTime(new Date());
                petOnlineAuditLog.setNote("审核失败，数据违规。。。");
                petOnlineAuditLogMapper.save(petOnlineAuditLog);

                throw new BusinessException("数据违规。。。");
            }
        }
    }


    /**
     * 宠物管理下架
     */
    @Override
    public void offsale(List<Long> ids) {
        Date offsaletime = new Date();
        petMapper.offsale(offsaletime, ids);
    }
}
